package org.veridiandynamics.client.view;

import java.awt.CardLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;
import java.util.Map;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.dao.Queryprocessor;
import org.veridiandynamics.server.domain.Admin;
import org.veridiandynamics.server.domain.Doctor;
import org.veridiandynamics.server.domain.Nurse;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * This class generates a gui screen accessed by system administrators 
 * that add users (whether they be nurses, doctors or other administrators 
 * to the database.
 * 
 * @author Lee
 */
public class AddUser extends JPanel {

	private Container container;
	private CardLayout c1;
	private JButton back;
	private JTextField usernameField;
	private JTextField passwordField;
	private JTextField firstNameField;
	private JTextField middleNameField;
	private JTextField lastNameField;
	private JTextField addressField;
	private JTextField cityField;
	private JTextField zipField;
	private JTextField homeTelField;
	private JTextField cellField;
	private JTextField emailField;
	String[] users;
	private JComboBox comboBox;
	private JButton btnAddUser;
	private JButton btnDeleteUser;
	private JButton btnEditUser;
	
	/**
	 * Constructor for AddUser.  Initializes everything on the gui.
	 * 
	 * @param c1
	 * @param container
	 */
	public AddUser(final CardLayout c1, final Container container)
	{
		this.c1 = c1;
		this.container = container;
		MainWindow.welcomeMessage.setText("Add, edit or delete user");
		
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(51dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(10dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(69dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(7dlu;default)"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("max(58dlu;default):grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
								
		JLabel lblPleaseEnterUsername = new JLabel("Please enter username");
		add(lblPleaseEnterUsername, "4, 4");
								
		JLabel lblPasswordifEditing = new JLabel("Password (if creating)");
		add(lblPasswordifEditing, "8, 4");
		
		usernameField = new JTextField();
		add(usernameField, "4, 6, fill, default");
		usernameField.setColumns(10);
		
		passwordField = new JTextField();
		add(passwordField, "8, 6, fill, default");
		passwordField.setColumns(10);
		
		JLabel lblPleaseEnterPersonal = new JLabel("Please enter personal information");
		add(lblPleaseEnterPersonal, "4, 8");
			
		JLabel lblFirstName = new JLabel("First Name");
		add(lblFirstName, "4, 10");
								
		JLabel lblMiddleName = new JLabel("Middle Name");
		add(lblMiddleName, "8, 10");
								
		JLabel lblLastName = new JLabel("Last Name");
		add(lblLastName, "12, 10");
								
		firstNameField = new JTextField();
		add(firstNameField, "4, 12, fill, default");
		firstNameField.setColumns(10);
								
		middleNameField = new JTextField();
		add(middleNameField, "8, 12, fill, default");
		middleNameField.setColumns(10);
								
		lastNameField = new JTextField();
		add(lastNameField, "12, 12, fill, default");
		lastNameField.setColumns(10);
								
		JLabel lblAddress = new JLabel("Address");
		add(lblAddress, "4, 14");
								
		JLabel lblCitystate = new JLabel("City/State");
		add(lblCitystate, "8, 14");
								
		JLabel lblZip = new JLabel("Zip");
		add(lblZip, "12, 14");
								
		addressField = new JTextField();
		add(addressField, "4, 16, fill, default");
		addressField.setColumns(10);
								
		cityField = new JTextField();
		add(cityField, "8, 16, fill, default");
		cityField.setColumns(10);
								
		zipField = new JTextField();
		add(zipField, "12, 16, fill, default");
		zipField.setColumns(10);
								
		JLabel lblHomeTelephone = new JLabel("Home telephone");
		add(lblHomeTelephone, "4, 18");
								
		JLabel lblCellphone = new JLabel("Cellphone");
		add(lblCellphone, "8, 18");
								
		JLabel lblEmailAddress = new JLabel("Email address");
		add(lblEmailAddress, "12, 18");
								
		homeTelField = new JTextField();
		add(homeTelField, "4, 20, fill, default");
		homeTelField.setColumns(10);
								
		cellField = new JTextField();
		add(cellField, "8, 20, fill, default");
		cellField.setColumns(10);
								
		emailField = new JTextField();
		add(emailField, "12, 20, fill, default");
		emailField.setColumns(10);
								
		JLabel lblWhatTypeOf = new JLabel("What type of user?");
		add(lblWhatTypeOf, "4, 22");
								
		JButton btnBack = new JButton("Back");
								
		btnBack.addActionListener(new ActionListener() 
		{
			/**
			 * This method moves the user back to the admin panel.
			 * 
			 * @param event The ActionEvent object handling the listening.
			 */
			public void actionPerformed(ActionEvent event) 
			{
				c1.first(container);
			}
		});
		users = new String[3];
		users[0] = "Nurse";
		users[1] = "Doctor";
		users[2] = "Admin";
		comboBox = new JComboBox(users);
		add(comboBox, "4, 24, fill, default");
		
		ButtonListener listener = new ButtonListener();
		
		btnAddUser = new JButton("Add User");
		btnAddUser.addActionListener(listener);
		add(btnAddUser, "4, 28");
		
		btnDeleteUser = new JButton("Delete User");
		btnDeleteUser.addActionListener(listener);
		add(btnDeleteUser, "8, 28");
		
		btnEditUser = new JButton("Edit User");
		btnEditUser.addActionListener(listener);
		add(btnEditUser, "12, 28");
		add(btnBack, "12, 36");
	}
	
	private class ButtonListener implements ActionListener
	{	
		public void actionPerformed(ActionEvent event)
		{
			String userType = users[comboBox.getSelectedIndex()];
			Map<String, String> personalInfo = new HashMap<String, String>();
			
			String username = usernameField.getText();
			String password = passwordField.getText();
			String fName = firstNameField.getText();
			String mName = middleNameField.getText();
			String lName = lastNameField.getText();
			String address = addressField.getText();
			String city = cityField.getText();
			String zip = zipField.getText();
			String telephone = homeTelField.getText();
			String cell = cellField.getText();
			String email = emailField.getText();
			
			personalInfo.put("username", username);
			personalInfo.put("password", password);
			personalInfo.put("first name", fName);
			personalInfo.put("middle name", mName);
			personalInfo.put("lName", lName);
			personalInfo.put("address", address);
			personalInfo.put("city", city);
			personalInfo.put("zip", zip);
			personalInfo.put("telephone", telephone);
			personalInfo.put("cell", cell);
			personalInfo.put("email", email);
			
			if (userType.equals("Nurse"))
			{
				if (event.getSource() == btnAddUser)
				{
					Nurse nurse = new Nurse(personalInfo);
					HibernateDriver driver = new HibernateDriver();
					driver.SaveObject(nurse);
					JOptionPane.showMessageDialog(null, "Nurse created!");
				}		
				if (event.getSource() == btnDeleteUser)
				{
					HibernateDriver driver = new HibernateDriver();
					Nurse nurse = new Nurse();
					long id = Queryprocessor.UsernametoID(username);
					nurse = (Nurse) driver.RetrieveObject(id, nurse);
					driver.DeleteObject(nurse);
					JOptionPane.showMessageDialog(null, "Nurse deleted!");
				}
				if (event.getSource() == btnEditUser)
				{
					Nurse nurse = new Nurse();
					HibernateDriver driver = new HibernateDriver();
					long id = Queryprocessor.UsernametoID(username);
					nurse = (Nurse) driver.RetrieveObject(id, nurse);
					if (!username.equals("")) nurse.setUsername(username);
					if (!password.equals("")) nurse.setPassword(password);
					if (!fName.equals("")) nurse.setFirstName(fName);
					if (!mName.equals("")) nurse.setMiddleName(mName);
					if (!lName.equals("")) nurse.setLastName(lName);
					if (!address.equals("")) nurse.setStreetAddress(address);
					if (!city.equals("")) nurse.setCity(city);
					if (!zip.equals("")) nurse.setZip(Integer.parseInt(zip));
					if (!telephone.equals("")) nurse.setTelephone(telephone);
					if (!cell.equals("")) nurse.setCell(cell);
					if (!email.equals("")) nurse.setEmail(email);
					driver.UpdateObject(nurse);
					JOptionPane.showMessageDialog(null, "Nurse edited!");
				}
			}
			
			if (userType.equals("Doctor"))
			{
				if (event.getSource() == btnAddUser)
				{
					Doctor doctor = new Doctor(personalInfo);
					HibernateDriver driver = new HibernateDriver();
					driver.SaveObject(doctor);
					JOptionPane.showMessageDialog(null, "Doctor created");
				}		
				if (event.getSource() == btnDeleteUser)
				{
					HibernateDriver driver = new HibernateDriver();
					Doctor doctor = new Doctor();
					long id = Queryprocessor.UsernametoID(username);
					doctor = (Doctor) driver.RetrieveObject(id, doctor);
					driver.DeleteObject(doctor);
					JOptionPane.showMessageDialog(null, "Doctor deleted");
				}
				if (event.getSource() == btnEditUser)
				{
					Doctor doctor = new Doctor();
					HibernateDriver driver = new HibernateDriver();
					long id = Queryprocessor.UsernametoID(username);
					doctor = (Doctor) driver.RetrieveObject(id, doctor);
					if (!username.equals("")) doctor.setUsername(username);
					if (!password.equals("")) doctor.setPassword(password);
					if (!fName.equals("")) doctor.setFirstName(fName);
					if (!mName.equals("")) doctor.setMiddleName(mName);
					if (!lName.equals("")) doctor.setLastName(lName);
					if (!address.equals("")) doctor.setStreetAddress(address);
					if (!city.equals("")) doctor.setCity(city);
					if (!zip.equals("")) doctor.setZip(Integer.parseInt(zip));
					if (!telephone.equals("")) doctor.setTelephone(telephone);
					if (!cell.equals("")) doctor.setCell(cell);
					if (!email.equals("")) doctor.setEmail(email);
					driver.UpdateObject(doctor);
					JOptionPane.showMessageDialog(null, "Doctor edited");
				}
			}
			
			if(userType.equals("Admin"))
			{
				if (event.getSource() == btnAddUser)
				{
					Admin admin = new Admin(personalInfo);
					HibernateDriver driver = new HibernateDriver();
					driver.SaveObject(admin);
					JOptionPane.showMessageDialog(null, "Admin created");
				}		
				if (event.getSource() == btnDeleteUser)
				{
					HibernateDriver driver = new HibernateDriver();
					Admin admin = new Admin();
					long id = Queryprocessor.UsernametoID(username);
					admin = (Admin) driver.RetrieveObject(id, admin);
					driver.DeleteObject(admin);
					JOptionPane.showMessageDialog(null, "Admin deleted");
				}
				if (event.getSource() == btnEditUser)
				{
					Admin admin = new Admin();
					HibernateDriver driver = new HibernateDriver();
					long id = Queryprocessor.UsernametoID(username);
					admin = (Admin) driver.RetrieveObject(id, admin);
					if (!username.equals("")) admin.setUsername(username);
					if (!password.equals("")) admin.setPassword(password);
					if (!fName.equals("")) admin.setFirstName(fName);
					if (!mName.equals("")) admin.setMiddleName(mName);
					if (!lName.equals("")) admin.setLastName(lName);
					if (!address.equals("")) admin.setStreetAddress(address);
					if (!city.equals("")) admin.setCity(city);
					if (!zip.equals("")) admin.setZip(Integer.parseInt(zip));
					if (!telephone.equals("")) admin.setTelephone(telephone);
					if (!cell.equals("")) admin.setCell(cell);
					if (!email.equals("")) admin.setEmail(email);
					driver.UpdateObject(admin);	
					JOptionPane.showMessageDialog(null, "Admin edited");
				}
			}
		}
	}
}
